package ninth.Java中线程的实现.Thread类和Runnable接口的区别;

class MyThread1 extends Thread {
    private int ticket = 5;

    public void run() {
        for (int i = 0; i < 100; i++) {
            if (ticket > 0) {
                System.out.println("卖票：ticket=" + ticket--);
            }
        }
    }
}

public class 继承Thread类不能资源共享 {
    public static void main(String[] args) {
        MyThread1 mt1 = new MyThread1();
        MyThread1 mt2 = new MyThread1();
        MyThread1 mt3 = new MyThread1();
        mt1.start();
        mt2.start();
        mt3.start();
    }
}
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=63124:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 ninth.Java中线程的实现.Thread类和Runnable接口的区别.继承Thread类不能资源共享
卖票：ticket=5
卖票：ticket=4
卖票：ticket=3
卖票：ticket=5
卖票：ticket=4
卖票：ticket=3
卖票：ticket=2
卖票：ticket=1
卖票：ticket=5
卖票：ticket=4
卖票：ticket=3
卖票：ticket=2
卖票：ticket=1
卖票：ticket=2
卖票：ticket=1

Process finished with exit code 0

 */
/**
 * 上面的程序通过Thread类启动了多个线程，但是多个线程之间只卖了各自的五张票，并没有达到资源共享的目的
 */